﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2011, hrondra
 */
using System;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;

using Close.Controls;
using Close.Properties;
using Close.Classes;
using System.Windows.Forms;
using Close.controls.MenuPanels;

namespace Close
{
    class LevelsPanel : FluidPanel
    {
        private FluidHeader header = new FluidHeader();
        private LevelListBox listBox = new LevelListBox();
        ThemeClass themeClass = ThemeClass.Settings;

        protected override void InitControl()
        {
            base.InitControl();
            //Bounds = new Rectangle(0, 0, 240, 300);
            BackColor = Color.Black;
            Anchor = AnchorAll;
            const int h = 32;
            header.Bounds = new Rectangle(0, 0, themeClass.DisplayWidth, h);
            listBox.Bounds = new Rectangle(0, h, themeClass.DisplayWidth, themeClass.DisplayHeight - h);
            header.Anchor = AnchorTLR;
            listBox.Anchor = AnchorAll;
            /*
            header.BackButton.Shape = ButtonShape.Rounded;
            header.BackButton.Visible = true;
            header.BackButton.Text = "OK";
            header.BackButton.Visible = false;
            */
            header.Title = Resources.DifTitle;
            //header.BackButton.Click += new EventHandler(BackButton_Click);

            Data.GameDatabase data = Data.GameDatabase.GamesData;
            listBox.DataSource = data.GetLevels();

            listBox.ItemClick +=new EventHandler<ListBoxItemEventArgs>(listBox_ItemClick); 

            Controls.Add(header);
            Controls.Add(listBox);

            FluidButton btnNumber = new FluidButton(Resources.ImportButton);
            btnNumber.Bounds = new Rectangle((themeClass.DisplayWidth - 125) / 2, themeClass.DisplayHeight - 30, 125, 20);
            //btnNumber.Bounds = new Rectangle(themeClass.DisplayHeight - 20, themeClass.DisplayWidth / 2 - 50, 150, 20);
            btnNumber.Click += ImportButton_Click;
            btnNumber.BackColor = Color.Black;
            btnNumber.ForeColor = Fluid.Classes.Theme.ThemeClass.Settings.NumpadForeColor;
            Controls.Add(btnNumber);

        }

        /// <summary>
        /// Import new suoku games from external file.
        /// </summary>
        void ImportButton_Click(object sender, EventArgs e)
        {
            PuzzleImporter importer = new PuzzleImporter();
            CloseSudoku.Forms.OpenFileDialog dialog = new CloseSudoku.Forms.OpenFileDialog();
            dialog.Filter = "*.opensudoku";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //save puzzles into program
                importer.Import(dialog.FileName);

                //reload (refresh) listbox
                Data.GameDatabase data = Data.GameDatabase.GamesData;
                listBox.DataSource = data.GetLevels();
            }
        }

        void BackButton_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Occurs when an item was clicked.
        /// </summary>
        public event EventHandler<ListBoxItemEventArgs> ItemClick;
        void listBox_ItemClick(object sender, ListBoxItemEventArgs e)
        {
            if (ItemClick != null)
            {
                ItemClick(this, e);
            }
        }

        public LevelDetail SelectedItem
        {
            get
            {
                return (LevelDetail)listBox.Item;
            }
        }
        public int SelectedIndex
        {
            get
            {
                return listBox.SelectedItemIndex;
            }
        }

        public string SelectedText
        {
            get
            {
                return listBox.SelectedText;
            }
        }
        public void Reload()
        {
            listBox.SelectedItemIndex = 0;
            listBox.DataSource = Data.GameDatabase.GamesData.GetLevels();
        }
        /*
        public List<LevelListBoxItem> Categories { get { return listBox.Categories; } }

        void BackButton_Click(object sender, EventArgs e)
        {
            ListBuilder.Instance.AddCategories(Categories);
            Close();
        }

        private static LevelsPanel instance;

        public static LevelsPanel Instance
        {
            get
            {
                if (instance == null) instance = new LevelsPanel();
                return instance;
            }
        }


        public override void Focus()
        {
            listBox.Focus();
        }


        internal void UpdateData()
        {
            listBox.UpdateData();
        }
         **/

    }
}
